package com.asset.filter;

import com.asset.pojo.Url;
import com.asset.service.UrlService;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;

import java.io.IOException;
import java.util.List;


public class BackendFilter implements Filter {


	protected UrlService urlService;

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;

	/*	String webName = request.getContextPath();// 当前被包含的war中真正的webName,即返回根目录
*/		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		ServletContext sc = request.getSession().getServletContext(); 
		XmlWebApplicationContext cxt = (XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(sc);

		if(cxt != null && cxt.getBean("urlServiceImp") != null && urlService == null){
			urlService = (UrlService) cxt.getBean("urlServiceImp");
		}
		String uri = request.getRequestURI();
		List<Url> urlList = urlService.listUrl();
		/*int flag = 0;
		HttpSession session = request.getSession();*/
		if(uri.contains("css") || uri.contains("js") || uri.contains("ajax") || uri.contains("jpg") || uri.equals("/")){
			//如果是css或者js文件直接给过ajax的请求action直接给过
		}else{
			if(uri.contains(".action")){				
			String uri1 = uri.replace(".action","");	
            String uri2 = uri.substring(uri.indexOf(".")+1, uri.indexOf(".")+7);
			if(checkURI(uri1,urlList) && uri2.equals("action")){
				
			}else{
				response.sendRedirect("/notFind");
				return;
			}
		 }else{
			 if(checkURI(uri,urlList)){

				
				}else{
					response.sendRedirect("/notFind");
					return;
				}
		 }
		}
		/*//如果页面不存在
		if(flag == 1){
			User user = (User)session.getAttribute(AppConstant.LOGIN_USER);
			if(user == null){
				response.sendRedirect(AppConstant.TO_LOGIN);
				return;
			}else{
				response.sendRedirect("/notFind");
				return;
			}
		}*/
//        String colId = request.getParameter("colId");
//		if (colId != null&&!"".equals(colId)) {
//			request.getSession().setAttribute("colId", colId);
//		}
		chain.doFilter(request, response);
	}
	public void init(FilterConfig arg0) throws ServletException {

	}
    
	public boolean checkURI(String uri,List<Url> urlList){
		for(int i= 0; i<urlList.size();i++){
			if(uri.equals(urlList.get(i).getUrl())){
				return true;				
			}				
		}
		return false;		
}
	
	
	public UrlService getUrlService() {
		return urlService;
	}

	public void setUrlService(UrlService urlService) {
		this.urlService = urlService;
	}

	public void destroy() {

	}
}
